﻿
/*
* Copyright (c) 2024, Pi上位机ui框架
* All rights reserved.
*
* 文件名称：Form1.h
* 文件标识：首页框架
* 摘 要：是一款基于 C# 开发的上位机软件 UI，旨在提供简洁直观的用户界面，并支持多种颜色风格切换功能，
* 满足不同用户的需求。该软件提供灵活的界面设计，适用于各种应用场景，尤其适合需要高效、
* 简洁界面的工业和自动化领域。
*
* 当前版本：1.1
* 作 者：李政沅
* 完成日期：2025年2月27日
*/



using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using PiCamera;
using PiControls.Controls.MaskLayer;
using PiFramework;
using PiUi.View;



namespace PiUi
{
    public partial class Form1 : Form
    {
        private Button currentButton;
        private Random random;
        private int tempIndex;
        private BaseView activeFrom;
        private Dictionary<string, BaseView> formDictionary;
        PMaskLayer maskLayer;
        public Form1()
        {
            InitializeComponent();
            random = new Random();
            // this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;  //取消全屏最大化
            this.Text = string.Empty;
            this.ControlBox = false;
            formDictionary = new Dictionary<string, BaseView>();
            BaseView AboutUi = new Froms.AboutView();
            formDictionary.Add("About", AboutUi);

            BaseView HomeUi2 = new Froms.HomeUi();
            formDictionary.Add("Home", HomeUi2);


            BaseView DebugUi = new Froms.DebugView();
            formDictionary.Add("Debug", DebugUi);


            BaseView DynamicsConfig = new DynamicsConfig();

            formDictionary.Add("Config",DynamicsConfig);


            BaseView ReportUi = new Froms.ReportView();
            formDictionary.Add("Report", ReportUi);
            BaseView SettingUi = new Froms.SettingView();
            formDictionary.Add("Setting", SettingUi);

            this.Paint += Form1_Paint;

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.Padding = new Padding(0, 0, 5, 0); // 去掉边框
            this.BackColor = Color.White; // 设置内部背景色
            int borderWidth = 5; // 边框宽度
            using (Pen pen = new Pen(ThemeColor.PrimaryColor, borderWidth)) // 红色边框
            {
                e.Graphics.DrawRectangle(pen,
                    borderWidth / 2, borderWidth / 2,
                    this.ClientSize.Width - borderWidth,
                    this.ClientSize.Height - borderWidth);
            }

        }

        [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
        private extern static void ReleaseCapture();
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private extern static void SendMessage(System.IntPtr hWnd, int msg, int wParam, int lParam);
        private Color SelectThemeColor()
        {
            tempIndex = (tempIndex + 1) % ThemeColor.m_ColorList.Count; // 防止溢出，循环回到 0
            string color = ThemeColor.m_ColorList[tempIndex];
            return ColorTranslator.FromHtml(color);

        }

        private void ActivateButtonNoChangeColor(object btnSender)
        {
            if (btnSender != null)
            {
                if (currentButton != (Button)btnSender) // 检查当前按钮是否与发送者按钮不同
                {
                    DisableButton(); // 禁用当前按钮
                    Color color = currentThemeColor(); // 获取当前主题颜色
                    currentButton = (Button)btnSender; // 将当前按钮设置为发送者按钮
                    currentButton.BackColor = color; // 设置发送者按钮的背景颜色为主题颜色
                    currentButton.ForeColor = Color.White; // 设置发送者按钮的前景颜色为白色
                    currentButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); // 设置发送者按钮的字体
                    panelTitleBar.BackColor = color; // 设置标题栏的背景颜色为主题颜色
                    panelLogo.BackColor = ThemeColor.ChangeColorBrightness(color, -0.3); // 设置Logo面板的背景颜色为比主题颜色暗30%的颜色
                    ThemeColor.PrimaryColor = color; // 设置主题的主要颜色为主题颜色
                    ThemeColor.SecondaryColor = ThemeColor.ChangeColorBrightness(color, -0.3); // 设置主题的次要颜色为比主题颜色暗30%的颜色
                }
            }




        }


        private Color currentThemeColor()
        {
            string color = ThemeColor.m_ColorList[tempIndex];
            return ColorTranslator.FromHtml(color);
        }


        private void DisableButton()
        {
            foreach (Control previousBtn in panelMenu.Controls)
            {

                if (previousBtn.GetType() == typeof(Button))
                {
                    previousBtn.BackColor = Color.FromArgb(51, 51, 76);
                    previousBtn.ForeColor = Color.Gainsboro;
                    previousBtn.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                }
            }
        }

        private void OpenChildFrom(BaseView childFrom, object btnSender)
        {

            childFrom.Hide();
            ActivateButtonNoChangeColor(btnSender);
            activeFrom = childFrom;
            childFrom.TopLevel = false;
            childFrom.FormBorderStyle = FormBorderStyle.None;
            childFrom.Dock = DockStyle.Fill;
            this.panelDesktopPane.Controls.Add(childFrom);
            this.panelDesktopPane.Tag = childFrom;
            childFrom.BringToFront();
            childFrom.Show();
            childFrom.LoadTheme();
            lbTitle.Text = childFrom.Text;


        }


        private void panelTitleBar_MouseDown_1(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, 0x112, 0xf012, 0);
        }

        private void close_Click(object sender, EventArgs e)
        {
            PJintThreadControl.Instance.AllThreadStop();
            PJintThreadControl.Instance.AllThreadDelete();
            PCameraControlHelp.Instance.CloseAllCamera();
            Application.Exit();
        }

        private void Max_Click(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Maximized;
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
            }


        }

        private void Min_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }


        private void Form1_Load(object sender, EventArgs e)
        {


            StartPosition = FormStartPosition.CenterScreen; //设置窗体启动位置
            if (formDictionary.ContainsKey("About"))
            {
                formDictionary.TryGetValue("About", out BaseView form);
                activeFrom = form;
                OpenChildFrom(activeFrom, this.buttonAbout);
            }

            maskLayer = new PMaskLayer();
            maskLayer.Text = "lMaskLayer1";
            maskLayer.Name = "lMaskLayer1";
            this.Controls.Add(this.maskLayer);
            this.StartPosition = FormStartPosition.CenterScreen;




            updateLanguage();

        }

        private void buttonAbout_Click(object sender, EventArgs e)
        {
            if (formDictionary.ContainsKey("About"))
            {
                formDictionary.TryGetValue("About", out BaseView form);
                activeFrom = form;
                OpenChildFrom(form, sender);
            }



        }



        private void buttonDebug_Click(object sender, EventArgs e)
        {
            //Debug
            if (formDictionary.ContainsKey("Debug"))
            {
                formDictionary.TryGetValue("Debug", out BaseView form);
                activeFrom = form;
                OpenChildFrom(form, sender);
            }

        }

        private void buttonHome_Click(object sender, EventArgs e)
        {
            if (formDictionary.ContainsKey("Home"))
            {
                formDictionary.TryGetValue("Home", out BaseView form);
                activeFrom = form;
                OpenChildFrom(form, sender);
            }
        }

        private void buttonConfig_Click(object sender, EventArgs e)
        {

            if (formDictionary.ContainsKey("Config"))
            {
                formDictionary.TryGetValue("Config", out BaseView form);
                activeFrom = form;
                OpenChildFrom(form, sender);
            }
        }

        private void buttonReport_Click(object sender, EventArgs e)
        {
            if (formDictionary.ContainsKey("Report"))
            {
                formDictionary.TryGetValue("Report", out BaseView form);
                activeFrom = form;
                OpenChildFrom(form, sender);

            }
        }

        private void buttonSetting_Click(object sender, EventArgs e)
        {

            if (formDictionary.ContainsKey("Report"))
            {
                formDictionary.TryGetValue("Setting", out BaseView form);
                activeFrom = form;
                OpenChildFrom(form, sender);
            }
        }

        private void buttonSetting_Click_1(object sender, EventArgs e)
        {
            if (formDictionary.ContainsKey("Setting"))
            {
                formDictionary.TryGetValue("Setting", out BaseView form);
                activeFrom = form;
                OpenChildFrom(form, sender);
            }
        }

        private void buttonTheme_Click(object sender, EventArgs e)
        {
            DisableButton();
            Color color = SelectThemeColor();
            currentButton.BackColor = color;
            currentButton.ForeColor = Color.White;
            currentButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            panelTitleBar.BackColor = color;
            panelLogo.BackColor = ThemeColor.ChangeColorBrightness(color, -0.3);
            ThemeColor.PrimaryColor = color;
            ThemeColor.SecondaryColor = ThemeColor.ChangeColorBrightness(color, -0.3);
            activeFrom.LoadTheme();
            this.Update();   // 立即处理重绘请求
            this.Invalidate(); // 仅重绘无效区域
        }



        private void button1_Click(object sender, EventArgs e)
        {
            this.panelMenu.Visible = !this.panelMenu.Visible;
        }

        private void buttonSetLanguage_Click(object sender, EventArgs e)
        {

            ThemeLanguage.ChangeLanguage();
            updateLanguage();



        }

        private void updateLanguage()
        {
            foreach (Control ctrl in GetAllControls(this))
            {
                string language = ThemeLanguage.GetString(ctrl.Name);

                if (!string.IsNullOrEmpty(language))
                {
                    ctrl.Text = language;
                }

            }

            foreach (var myform in formDictionary.Values)
            {
                foreach (Control ctrl in GetAllControls(myform))
                {
                    string language = ThemeLanguage.GetString(ctrl.Name);

                    if (!string.IsNullOrEmpty(language))
                    {
                        ctrl.Text = language;
                    }
                }
            }
        }


        private IEnumerable<Control> GetAllControls(Control control)
        {
            foreach (Control child in control.Controls)
            {
                // 返回当前子控件
                yield return child;
                // 递归返回子控件的子控件
                foreach (Control descendant in GetAllControls(child))
                {
                    yield return descendant;
                }
            }
        }
    }
}
